package com.wu.dms.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.convertion.errorcode.SingerErrorCode;
import com.wu.common.convertion.exception.ServiceException;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.dms.dao.entity.Singer;
import com.wu.dms.dao.mapper.SingerMapper;
import com.wu.dms.dto.SingerRequestDTO;
import com.wu.dms.service.SingerService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * @author Cousin
 */
@Service
@RequiredArgsConstructor
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {


    private final MinIoUtil minIoUtil;

    @Override
    public void updateSingerMsg(SingerRequestDTO requestParam) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(requestParam, singer);
        try {
            updateById(singer);
        } catch (Exception e) {
            log.error(e.toString());
            throw new ServiceException(SingerErrorCode.SINGER_UPDATE_ERROR);
        }


    }

    @SneakyThrows
    @Override
    public void updateSingerPic(MultipartFile avatarFile, Long id) {
        String fileName = avatarFile.getOriginalFilename();
        if (MinIoNameEnum.SINGER_IMAGE.getDefaultObjectName().equals(fileName)){
            return;
        }
        Singer singer = query().eq("id", id).one();
        if (singer == null) {
            return;
        }
        String actorAvatarImage = singer.getPic();
        minIoUtil.deleteFile(MinIoNameEnum.SINGER_IMAGE.getBucketName(), MinIoNameEnum.getDefaultObjectName(id,actorAvatarImage));
        minIoUtil.uploadFile(MinIoNameEnum.SINGER_IMAGE.getBucketName(), MinIoNameEnum.getDefaultObjectName(id,fileName), avatarFile.getInputStream(), avatarFile.getSize());
        LambdaUpdateWrapper<Singer> updateWrapper = Wrappers.lambdaUpdate(Singer.class)
                .eq(Singer::getId, id)
                .set(Singer::getPic, fileName);
            update(updateWrapper);
    }

    @Override

    public void deleteSinger(Long id) {
        try {
            removeById(id);
        } catch (Exception e) {
            log.error(e.toString());
            throw new ServiceException(SingerErrorCode.SINGER_DELETE_ERROR);
        }


    }
    @Override

    public void addSinger(SingerRequestDTO requestParam) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(requestParam, singer);
        String pic = MinIoNameEnum.SINGER_IMAGE.getDefaultObjectName();
        singer.setPic(pic);
        this.save(singer);
    }

    @Override
    public List<Singer> actorOfName(String name) {

        return query().like("name", name).list();
    }

    @Override
    public List<Singer> actorOfSex(Integer sex) {
        return query().eq("sex", sex).list();
    }
}
